<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">

                <el-form-item label="审核人员" prop="name">
                    <el-select v-model="formData.member_id" filterable remote placeholder="输入人员姓名" allow-create
                        :remote-method="remoteMethod" :loading="loading">
                        <el-option v-for="item in Userlinks" :key="item.value" :label="item.name" :value="item.value" />
                    </el-select>
                </el-form-item>

                <el-form-item label="审批状态">
                    <el-switch v-model="formData.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { flowEdit, flowAdd, flowDetail, flowAll, flowSearchUser } from '@/api/org/flow'
import Popup from '@/components/popup/index.vue'
import { useDictOptions } from '@/hooks/useDictOptions'
import request from '@/utils/request'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑流程' : '新增流程'
})


const Userlinks = ref([])

/**删除流程用户 */
const deptUserDel = (item: any) => {
    console.log('Selected item:', item);
}
// 远程姓名检索
const loading = ref(false)
const remoteMethod = async (query: string) => {

    if (query) {
        Userlinks.value = await flowSearchUser(query)
        loading.value = true
        setTimeout(() => {

            console.log('Userlinks', Userlinks.value);
            loading.value = false
        }, 200)
    } else {
        Userlinks.value = []
    }
}

const formData = reactive({
    id: '',
    fid: 0 as number,
    pid: 0 as string | number,
    deptid: 0 as number,
    name: '',
    leader: '',
    flowtype: 1,
    sort: 0,
    member_id: '',
    deptUser: [] as any,
    status: 1
})


const formRules = {
    // pid: [
    //     {
    //         required: true,
    //         message: '请选择上级流程',
    //         trigger: ['change']
    //     }
    // ],
    member_id: [
        {
            required: true,
            message: '请选择审批人员',
            trigger: ['blur']
        }
    ]
}

const { optionsData } = useDictOptions<{
    dept: any[]
}>({
    dept: {
        api: flowAll
    }
})

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await flowEdit(formData) : await flowAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await flowDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
